<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopProductGroupBean {

    function BWebshopProductGroupBean() {
    	
    }	
    
    public function get_all_product_group($db, $sort, $by) {
    	if($sort!="")
			$condsort = "ORDER BY PG.".$sort." ".$by;
		else
			$condsort = "ORDER BY PG.product_group_id ";
		$sql = "SELECT * FROM " . TBL_PRODUCT_GROUP . " PG " . $condsort;
    	$res = $db->get_results($sql);
    	return $res;
	}
	
	public function get_all_product_group_paging($db,$paging,$path,$sort, $by) {
    	if($sort!="")
			$condsort = "ORDER BY PG.".$sort." ".$by;
		else
			$condsort = "ORDER BY PG.product_group_id ";
		$page = isset ( $_GET[$path] ) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
			
		$sql = "SELECT * FROM " . TBL_PRODUCT_GROUP . " PG " . $condsort." LIMIT $page_start, $rows_per_page";
    	$res = $db->get_results($sql);
    	return $res;
	}
	
	public function get_product_list_by_id($db, $id) {
		$sql = "SELECT * FROM " . TBL_PRODUCT_GROUP . " progr WHERE progr.product_group_id = '$id'";
    	$res = $db->get_row($sql);
    	return $res;
	}
	
	public function validate_product_list() {
    	$msg = "";
		if(trim($_POST['product_group_name']) == "") {
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter product list name.");
		}
		return $msg;
    }
	
	public function save_product_list($db, $arr_productlist) {
		$sql=sprintf(
					"INSERT INTO %s(product_group_name,product_group_description) ".
					"VALUES('%s','%s')",
    				TBL_PRODUCT_GROUP,
					$arr_productlist['product_group_name'],
    				$arr_productlist['product_group_description']);
    				
    	$res = $db->query($sql);
    	if($res == true) {
    		return $db->insert_id;
    	} else
    		return -1;
	}
	
	public function delete_product_list_by_id($db, $id) {
    	$sql = sprintf("DELETE FROM %1\$s WHERE product_group_id ='$id'", TBL_PRODUCT_GROUP);		
    	$res = $db->query($sql);
    	return $res;
    }
	
	public function update_product_list($db, $arr_productlist) {
		$sql = sprintf("UPDATE  %s SET" .
    					" product_group_name = '%s'," .
    					" product_group_description = '%s'" .
    					" WHERE product_group_id = '%s'", 
					TBL_PRODUCT_GROUP,
					$arr_productlist['product_group_name'],
					$arr_productlist['product_group_description'],
					$arr_productlist['product_group_id']);
		$res = $db->query($sql);
    	return $res;
	}
	
	public function get_productlist2cat_by_catid($db, $id) {
		$sql = "SELECT * FROM " . TBL_PRODUCT_GROUP2CAT . " p WHERE p.cat_id = '$id'";
    	$res = $db->get_row($sql);
    	return $res;
	}
	
	public static function get_all_product_group_to_assign($db,$catid) {
    	$strSQL = sprintf("SELECT l.product_group_name,l.product_group_id,(SELECT t.product_group_id 
    			FROM ".TBL_PRODUCT_GROUP2CAT." t WHERE t.product_group_id = l.product_group_id AND t.cat_id = '$catid' LIMIT 0,1) as active_product_group".
				" FROM ".TBL_PRODUCT_GROUP." l WHERE (SELECT SUM(p.product_id) FROM ".TBL_PRODUCT." p WHERE p.product_group_id = l.product_group_id )>'0'");
    	$res = $db->get_results($strSQL);
    	return $res;
    }
	
	public function save_assign_product($db,$product_id,$cat_id,$product_group_id){
    	$res = $db->query(sprintf("INSERT INTO %s" .
									"(product_id,product_group_id, cat_id) " .
									"VALUES('%s','%s', '%s')",
			    				TBL_PRODUCT_GROUP2CAT,$product_id, $product_group_id, $cat_id));
    }
	
	public function update_assign_product_list($db,$product_group_id,$cat_id){
		
		$res = $db->query(sprintf("UPDATE  ".TBL_PRODUCT_GROUP2CAT." SET product_group_id =".$product_group_id." WHERE cat_id = ".$cat_id));
    	
    }
	
	public function delete_assign_product($db,$product_id,$cat_id,$product_group_id){
		if($product_id!="")
			$condpro = "AND product_id = '$product_id'";
		
		$res = $db->query(sprintf("DELETE FROM ".TBL_PRODUCT_GROUP2CAT." WHERE cat_id = '$cat_id' AND product_group_id = '$product_group_id' $condpro"));
    	
    }
	
}
?>